package com.tdcoding.chat.action.common;

import java.lang.reflect.Method;
import java.util.ArrayList;

import com.tdcoding.chat.action.BaseAction;
import com.tdcoding.chat.entities.ChatInfo;
import com.tdcoding.chat.entities.ResponseInfo;
import com.tdcoding.chat.helper.Logger;
import com.tdcoding.chat.helper.ProtostuffHelper;
import com.tdcoding.chat.listener.IChannelChatListener;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;

/**
 * 
 * Action的封装类
 * 
 * @author wellsa
 *
 */
public class ActionContext {
	private static final String TAG = "ActionContext";
	private ArrayList<ActionFilter> lBaseFilters; // 过滤器
	private BaseAction baseAction; // Action对象Object
	private Method method; // 处理业务方法

	public ArrayList<ActionFilter> getlBaseFilters() {
		return lBaseFilters;
	}

	public void setlBaseFilters(ArrayList<ActionFilter> lBaseFilters) {
		this.lBaseFilters = lBaseFilters;
	}

	public BaseAction getBaseAction() {
		return baseAction;
	}

	public void setBaseAction(BaseAction baseAction) {
		this.baseAction = baseAction;
	}

	public Method getMethod() {
		return method;
	}

	public void setMethod(Method method) {
		this.method = method;
	}

	/**
	 * 通知用户状态信息
	 * 
	 * @param userId
	 * @param userStatus
	 *            0正常；1隐身；2离开；3忙碌；4勿打扰；5离线
	 */
	public static void dispatchMyStatus(long userId, int status) {
		Logger.println(TAG, "dispatchMyStatus", userId, status);
//		ArrayList<FriendInfo> lUserInfos = DBForUser.queryFriendList(userId);
//		ArrayList<Serializable> lChannelInfos = ChatManager
//				.getUserChannelInfos(lUserInfos);
//		for (Serializable serializable : lChannelInfos) {
//			ChannelInfo channelInfo = (ChannelInfo) serializable;
//			Channel fChannel = channelInfo.getChatChannel();
//			if (fChannel != null && fChannel.isActive()) {// 是否在线
//				StatusInfo statusInfo =new StatusInfo();
//				statusInfo.setUserId(channelInfo.getUserId());
//				statusInfo.setStatus(status);
//				writeResponse(fChannel, null, ChatConstants.UPDATE_STATUS, 0,
//						statusInfo);
//			}
//		}
	}

	/**
	 * 回复单通知
	 * 
	 * @param chatInfo
	 */
//	public static int chattingToUser(ChatInfo chatInfo,
//			long chatToId) {
//		Logger.println(TAG, "chattingToUser", chatInfo);
//		int result = DBForChat.addChatInfo(ChatConstants.MSG_SEND_SENDING,
//				chatInfo);
//		if (result==-1) {
//			return -1;
//		}
//		ChannelInfo channelInfo = ChatManager.getChannelInfo(chatToId);
//		if (channelInfo==null) {
//			return 0;
//		}
//		Channel toChannel = channelInfo.getChatChannel();
//		if (toChannel != null
//				&& toChannel.isActive()
//				&& channelInfo.getStatus() == MainConstants.USER_STATUS_ONLINE) {// 在线
//			Logger.println(TAG, "chattingToUser online", result);
//			handleResponse(toChannel, chatInfo.getPacketId(),
//					ChatConstants.MSG_RECEIVE,
//					ChatConstants.MSG_SEND_SENDING, chatInfo);
//		}
//		return 0;
//	}

	/**
	 * 分发群通知
	 * 
	 * @param chatInfo
	 * @param lMemberInfos
	 */
//	public static int dispatchGroupNotice(ChatInfo chatInfo,
//			List<MemberInfo> lMemberInfos) {
//		Logger.println(TAG, "dispatchGroupNotice", chatInfo, lMemberInfos);
//		int sendFailure=0;
//		List<ChannelInfo> lChannelInfos = ChatManager
//				.getMemberChannelInfos(lMemberInfos);
//		for (ChannelInfo channelInfo : lChannelInfos) {
//			chatInfo.setPacketId(StringHelper.packetId());
//			chatInfo.setChatToRid(channelInfo.getUserId());
//			int result = DBForChat.addChatInfo(ChatConstants.MSG_SEND_SENDING,
//					chatInfo);
//			if (result==0) {
//				Channel toChannel = channelInfo.getChatChannel();
//				if (toChannel != null
//						&& toChannel.isActive()&& channelInfo.getStatus() == MainConstants.USER_STATUS_ONLINE) {// 不在线
//					handleResponse(toChannel, chatInfo.getPacketId(),
//							ChatConstants.MSG_RECEIVE,ChatConstants.MSG_SEND_SENDING, chatInfo);
//				}
//			}
//			else {
//				sendFailure=sendFailure+1;
//			}
//		}
//		if (sendFailure==lChannelInfos.size()) {
//			return -1;
//		}
//		return 0;
//	}

	/**
	 * 响应请求
	 * 
	 * @param channel
	 * @param packetId
	 * @param respCode
	 * @param chatInfo
	 */
	public static void handleResponse(Channel channel, String responeId,
			String method, int respCode, ChatInfo chatInfo) {
		Logger.println(TAG, "handleChat", responeId, respCode, chatInfo);
		ChannelFuture future =writeResponse(channel, responeId, method, respCode, chatInfo);
		future.addListener(new IChannelChatListener(chatInfo));
	}
	
	/**
	 * 响应请求
	 * 
	 * @param channel
	 * @param packetId
	 * @param respCode
	 * @param respone
	 */
	public static ChannelFuture writeResponse(Channel channel, String packetId,
			String method, int respCode, Object... respData) {
		Logger.println(TAG, "writeResponse", packetId, respCode, respData);
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setMethod(method);
		responseInfo.setPacketId(packetId);
		responseInfo.setRespCode(respCode);
		responseInfo.setRespData(respData);
		byte[] bytes=ProtostuffHelper.serialize(responseInfo);
		ChannelFuture future = channel.writeAndFlush(bytes);
		return future;
	}
	
	/**
	 * 处理响应
	 * @param reason
	 */
	public static ChannelFuture writeResponse(Channel channel,String packetId,String method,String reason) {
		Logger.println(TAG, "handleRespone", channel,packetId,method,reason);
		ResponseInfo responseInfo=new ResponseInfo();
		responseInfo.setMethod(method);
		responseInfo.setPacketId(packetId);
		responseInfo.setRespCode(-1);
		responseInfo.setRespData(new Object[]{reason});
		byte[] bytes=ProtostuffHelper.serialize(responseInfo);
		ChannelFuture future=channel.writeAndFlush(bytes);
		return future;
	}

}